
var itemlist = new Array("tonic", "potion", "phoenix_down", "eye_drop", "antidote", "lletus", "cll", "cln", "psoup", "rhodox", "rhodoxc", "bob", "bburger", "pps", "rstew", "mmuu", "mcandy", "gbcr", "fee", "hbox", "pcorn", "cc", "vmeal");
var items = new Array();

items["tonic"] = new Object();
items["tonic"].name = "Tonic";
items["tonic"].quantity = 10;
items["tonic"].order = 0;
items["tonic"].effects = [50,0,0];

/*	---------
	EFFECTS: {hp, mp, [#status, mode], script}
	--------
	hp:
	[
		RANGE	:ACTION	[] = inclusive. () = exclusive.
		[-~, 0)	:Subtracts custom amount. Ex: -11, -825, -9998
		[0]		:None added
		(0,1)	:Percentage Reduction. Ex: .15, .20, .75
		[1]		:Sets HP to 1
		(1,2)	:Percentage Addition. Ex: 1.20, 1.80
		[2]		:Sets HP to max
		[3]		:Sets HP to 0
		[4]		:Halves cur hp
		[5,~]	:Adds custom amount. Ex: 5, 25, 61
	]
	mp:
	[
		mp works the same as hp
	]
	#status:
	[
		0: None
		1: Blind
		2: Confused
		3: Stoned.
		4: Drunk
		5: Poison (really drunk)
		6: Etc.
		You can also use string literals as defined in Statii. Ex: "Poison", "Blind", etc
		
		mode:
		[
			-1	:Toggles status effect
			 0	:Clears status ailment (off)
			 1	:Induces status (on) (default)
		]
	]
	script:
	[
		whateva ya want!
	]
*/

function USE_ITEM(g,w)
{
	if (items[g].quantity < 1) return;	//this shouldn't happen but..
	
	if (g == "phoenix_down")
		if (charstats[w].hp < 1)
			charstats[w].hp = 1;
		else return "He ain't dead yet!";
	
	var hp = items[g].effects[0];
	var mp = items[g].effects[1];
	var thp, tmp;
	var hp_dif, mp_dif;
	
	if (hp > 0)
		if (charstats[w].hp < 1)
			return "He's dead!";
	thp = charstats[w].hp;
	if (hp && charstats[w].hp >= charstats[w].hp_max) return "That's all yer gettin'!";
	if (hp < 0) charstats[w].hp += hp;
	if (hp==0) ;
	if (hp > 0 && hp < 1) charstats[w].hp *= hp;
	if (hp==1) charstats[w].hp = 1;
	if (hp > 1 && hp < 2) charstats[w].hp *= hp;
	if (hp==2) charstats[w].hp = charstats[w].hp_max;
	if (hp==3) charstats[w].hp = 0;
	if (hp==4){ charstats[w].hp /= 2; charstats[w].hp = Math.round(charstats[w].hp); }
	if (hp>=5) charstats[w].hp += hp;
	
	if (charstats[w].hp > charstats[w].hp_max) charstats[w].hp = charstats[w].hp_max;
	hp_dif = charstats[w].hp_max - thp;

	if (mp && charstats[w].mp >= charstats[w].mp_max) return "Save some for later.";
	if (mp < 0) charstats[w].mp += hp;
	if (mp==0) ;
	if (mp > 0 && mp < 1) charstats[w].mp *= mp;
	if (mp==1) charstats[w].mp = 1;
	if (mp > 1 && mp < 2) charstats[w].mp *= mp;
	if (mp==2) charstats[w].mp = charstats[w].mp_max;
	if (mp==3) charstats[w].mp = 0;
	if (mp==4){ charstats[w].mp /= 2; charstats[w].mp = Math.round(charstats[w].mp); }
	if (mp>=5) charstats[w].mp += mp;
	
	if (charstats[w].mp > charstats[w].mp_max) charstats[w].mp = charstats[w].mp_max;
	mp_dif = charstats[w].mp_max - tmp;
	
	if (typeof items[g].effects[2] == "string")
		charstats[w][items[g].effects[2]] = true;
	else if (typeof items[g].effects[2] == "object")	//[x,y]
	{
		var stat = items[g].effects[2];
		if (stat[0] == "string") 		// possible to have ["Poison", -1]
		{
			if (stat[1]==-1) charstats[w][stat[0]] = !charstats[w][stat[0]]
			if (stat[1]== 0) charstats[w][stat[0]] = false;
			if (stat[1]== 1) charstats[w][stat[0]] = true;
		}
		else						// it's [3, 1] or somethin
		{
			if (stat[1]==-1) charstats[w][Statii[stat[0]]] = !charstats[w][stat[0]]
			if (stat[1]== 0) charstats[w][Statii[stat[0]]] = false;
			if (stat[1]== 1) charstats[w][Statii[stat[0]]] = true;
		}
	}
	items[g].quantity--;
	
	if (g=="phoenix_down") return "1";
	if (items[g].effects[0]) return hp_dif;	//return hp added (phoenix down gives 1hp)
	if (items[g].effects[1]) return mp_dif;
	return 0;
}

var Statii = new Array("LOL", "Blind", "Confused", "Drunk", "Poison", "Etc");

items["potion"] = new Object();
items["potion"].name = "Potion";
items["potion"].quantity = 4;
items["potion"].order = 1;
items["potion"].effects = new Array(200,0,0);

items["ether"] = new Object();
items["ether"].name = "Ether";
items["ether"].quantity = 1;
items["ether"].order = 22;
items["ether"].effects = new Array(0,100,0);

items["phoenix_down"] = new Object();
items["phoenix_down"].name = "Phoenix Down";
items["phoenix_down"].quantity = 2;
items["phoenix_down"].order = 2;
items["phoenix_down"].effects = [0, 0, 0];	//set hp to 1, IF THEYRE DEAD

items["eye_drop"] = new Object();
items["eye_drop"].name = "Eye Drop";
items["eye_drop"].quantity = 3;
items["eye_drop"].order = 3;
items["eye_drop"].effects = [0,0, ["Blind", 0]];

items["antidote"] = new Object();
items["antidote"].name = "Antidote";
items["antidote"].quantity = 2;
items["antidote"].order = 4;
items["antidote"].effects = [0,0, ["Poison", 0]];

items["lletus"] = new Object();
items["lletus"].name = "Leafer Letus";
items["lletus"].quantity = 0;
items["lletus"].order = 5;

items["cll"] = new Object();
items["cll"].name = "Chicken Lip Legs";
items["cll"].quantity = 0;
items["cll"].order = 6;

items["cln"] = new Object();
items["cln"].name = "Chicken Lip Nuggets";
items["cln"].quantity = 0;
items["cln"].order = 7;

items["psoup"] = new Object();
items["psoup"].name = "Pug Soup";
items["psoup"].quantity = 0;
items["psoup"].order = 5;

items["rhodox"] = new Object();
items["rhodox"].name = "Rhodox";
items["rhodox"].quantity = 0;
items["rhodox"].order = 8;

items["rhodoxc"] = new Object();
items["rhodoxc"].name = "Rhodox with Cheese";
items["rhodoxc"].quantity = 0;
items["rhodoxc"].order = 9;

items["bob"] = new Object();
items["bob"].name = "Were-Rat Kabob";
items["bob"].quantity = 0;
items["bob"].order = 10;

items["bburger"] = new Object();
items["bburger"].name = "Behemoth Burger";
items["bburger"].quantity = 0;
items["bburger"].order = 11;

items["pps"] = new Object();
items["pps"].name = "Peppered Peepers";
items["pps"].quantity = 0;
items["pps"].order = 12;

items["rstew"] = new Object();
items["rstew"].name = "Rhobite Stew";
items["rstew"].quantity = 0;
items["rstew"].order = 13;

items["mmuu"] = new Object();
items["mmuu"].name = "Mashed Muu";
items["mmuu"].quantity = 0;
items["mmuu"].order = 14;

items["mcandy"] = new Object();
items["mcandy"].name = "Mind Candy";
items["mcandy"].quantity = 0;
items["mcandy"].order = 15;

items["gbcr"] = new Object();
items["gbcr"].name = "Gold Bear Crunch Bar";
items["gbcr"].quantity = 0;
items["gbcr"].order = 16;

items["fee"] = new Object();
items["fee"].name = "Fries of FlameEater";
items["fee"].quantity = 0;
items["fee"].order = 17;

items["hbox"] = new Object();
items["hbox"].name = "Hidonites in a Box";
items["hbox"].quantity = 0;
items["hbox"].order = 18;

items["pcorn"] = new Object();
items["pcorn"].name = "Poppers Corn";
items["pcorn"].quantity = 0;
items["pcorn"].order = 19;

items["cc"] = new Object();
items["cc"].name = "Cactrot Cola";
items["cc"].quantity = 0;
items["cc"].order = 20;

items["vmeal"] = new Object();
items["vmeal"].name = "Value McMeal";
items["vmeal"].quantity = 0;
items["vmeal"].order = 21;

var Black 		=CreateColor(0,   0,   0,   255);
var White		=CreateColor(255, 255, 255, 255);
var Red 		=CreateColor(255, 0,   0,   255);
var Green		=CreateColor(0,   255, 0,   255);
var Blue 		=CreateColor(0,   0,   255, 255);
var Cyan 		=CreateColor(0,   255, 255, 255);
var Magenta	=CreateColor(255, 0,   255, 255);
var Yellow		=CreateColor(255, 255, 0,   255);
var Cyan		=CreateColor(255, 255, 0,   255);//CreateColor(255, 220, 220, 200);
var halfTint		=CreateColor(255,255,255, 128);
var Grey		=CreateColor(128, 128, 128, 255);


/*
//==============\\
     --- WEAPONS --- 
\\==============//
*/


var weaponlist = new Array("megaflail", "bastard_sword", "mace", "buster_spear", "boombox", "nuke", "rake_of_fire");
var weapstats = new Array('obj_id', 'name', 'extremity', 'obtrusiveness', 'weight', 'sharpness', 'polish');
var weapons = new Array();

function init_weapons()
{
	utility(["megaflail", "MegaFlail", 500, 200, 19, 12, 5]);
	utility(["bastard_sword", "Bastard Sword", 100, 15, 15, 20, 30]);
	utility(["mace", "Mace", 2, 3, 5, 1, 6]);
	utility(["buster_spear", "Buster Spear", 250, 300, 100, 50, 2000]);
	utility(["boombox", "Boombox", 9, 60, 75, 1, 15]);
	utility(["nuke", "Nuclear Weapon", 9999, 7777, 6502, 1, 18]);
	utility(["rake_of_fire", "Rake of Fire", 500, 25, 6, 50, 2]);
}
	
function utility(digits)
{
	weapons[digits[0]] = new Object();
	for (var d=0; d<weapstats.length; d++)
		weapons[digits[0]][weapstats[d]] = digits[d];
}



/*
//====================\\
 --- CHARACTER STATISTICS --- 
\\====================//
*/


var charlist = new Array("Kefka", "Vicks", "Wedge", "Gau", "Piette", "Valaria", "Sephiroth", "Magus",
	"Ultros", "Reiker", "Pepper", "Mr. Chupon", "Greenman", "R-666I", "Spike", "Cid", "Zigfreid", "Lonewolf",
	"Vargas", "Maria", "Thompson", "Smitty", "Ziggy","Greasemonk");	//remove pirates
var charstats = new Array();
var statlist = new Array("name", "level", "hp", "hp_max", "mp", "mp_max", "vigor", "speed", "stamina",
	"magpow", "batpow", "defense", "evade", "magdef", "mblock", "exp", "rss", "facepic", "rhand", "lhand",
	"head", "body", "relic1", "relic2", "status");
var charspells = new Array();
var charshards = new Array();
var i;
var j;
for( i = 0; i < charlist.length; i++ ) {
	charspells[charlist[i]] = new Array();
	for( j = 0; j < 3*(7+6+6); j++ ) {
		charspells[charlist[i]][j] = 0;
		charshards[j] = 4;
	}
}

function init_charstats()
{
	//dostats(["Kefka", 1, 78, 78, 39, 39, 27, 37, 24, 48, 22, 25, 4, 77, 8, 0, "Kefka.rss"]);
	dostats(["Kefka", 20, 150, 150, 175, 175, 50, 60, 80, 75, 55, 61, 10, 125, 25, 5, "Kefka.rss", "Face01.png"]);
	dostats(["Vicks", 1, 70, 70, 24, 24, 41, 36, 45, 28, 40, 44, 3, 31, 0, 0, "Brown.rss", "Face04.png"]);
	dostats(["Wedge", 1, 68, 68, 31, 31, 40, 35, 46, 29, 39, 51, 2, 55, 0, 0, "Brown.rss", "Face03.png"]);
	dostats(["Piette", 1, 57, 57, 44, 44, 35, 37, 52, 39, 33, 31, 6, 41, 1, 0, "Piette.rss", "Face02.png"]);
	dostats(["Valaria", 1, 55, 55, 46, 46, 38, 33, 41, 35, 40, 29, 4, 51, 2, 0, "Valaria.rss", "Face05.png"]);
	dostats(["Sephiroth", 1, 71, 71, 40, 40, 46, 21, 41, 32, 47, 24, 7, 66, 5, 0, "Seph.rss", "Face10.png"]);
	dostats(["Magus", 1, 49, 49, 55, 55, 29, 37, 27, 49, 25, 27, 12, 51, 10, 0, "Magus.rss", "Face11.png"]);
	dostats(["Ultros", 1, 61, 61, 27, 27, 40, 33, 27, 25, 37, 22, 0, 22, 0, 0,  "Ultros.rss", "Face95.png"]);
	dostats(["Reiker", 1, 63, 63, 31, 31, 58, 41, 37, 11, 42, 23, 15, 13, 0, 0, "Reiker.rss", "Face13.png"]);
	dostats(["Pepper", 1, 62, 62, 37, 37, 59, 39, 40, 31, 35, 25, 5, 31, 2, 0, "Greenette.rss", "Face07.png"]);
	dostats(["Mr. Chupon", 1, 71, 71, 11, 11, 23, 31, 66, 25, 46, 20, 16, 44, 18, 0, "Chupon.rss", "Face97.png"]);
	dostats(["Greenman", 1, 35, 35, 96, 96, 37, 28, 26, 35, 33, 25, 5, 25, 5, 0, "Greenman.rss", "Face98.png"]);
	dostats(["R-666I", 1, 88, 88, 4, 4, 54, 24, 78, 13, 67, 20, 7, 41, 0, 0, "R66I.rss", "Face96.png"]);
	dostats(["Spike", 1, 52, 52, 25, 25, 41, 35, 21, 34, 34, 35, 12, 21, 5, 0, "Spike.rss", "Greasemonk.png"]);
	dostats(["Cid", 1, 51, 51, 47, 47, 31, 28, 35, 31, 29, 34, 10, 33, 10, 0, "Cid.rss", "Face09.png"]);
	dostats(["Zigfreid", 1, 47, 47, 31, 31, 34, 38, 23, 31, 29 ,32, 10, 24, 0, 0, "Sigfreid.rss", "Face96.png"]);
	dostats(["Lonewolf", 1, 37, 37, 46, 46, 41, 47, 31, 21, 25, 17, 19, 11, 8, 0, "Lonewolf.rss", "Face99.png"]);
	dostats(["Vargas", 1, 51, 51, 21, 21, 44, 28, 49, 21, 41, 45, 7, 33, 11, 0, "Vargas.rss", "Face08.png"]);
	dostats(["Maria", 1, 24, 24, 27, 27, 36, 44, 21, 54, 21, 27, 32, 37, 11, 0, "Maria.rss", "Face94.png"]);
	// misc chars
	dostats(new Array("Mog", 1, 62, 62, 0, 0, 32, 40, 35, 44, 51, 106, 32, 93, 5, 0, "Mog.rss", "Humpty.png"));
	dostats(new Array("Gau", 1, 68, 68, 0, 0, 40, 35, 46, 29, 62, 132, 25, 86, 0, 0, "Gau.rss", "Rhodox.png"));
	dostats(new Array("Thompson", 1, 68, 68, 0, 0, 40, 35, 46, 29, 62, 135, 25, 86, 0, 0, "Thompson.rss", "Face94.png"));	//....heh
	dostats(new Array("Smitty", 1, 62, 62, 0, 0, 32, 40, 35, 44, 51, 106, 32, 93, 5, 0, "Theif01.rss", "Face14.png"));	//remove
	dostats(new Array("Ziggy", 1, 62, 62, 0, 0, 32, 40, 35, 44, 51, 106, 32, 93, 5, 0, "Theif01.rss", "Face14.png"));	//get ridda
	dostats(['Greasemonk', 1, 62, 62, 0, 0, 32, 40, 35, 44, 51, 106, 32, 93, 5, 0, "Mechanic.rss", "Face98.png"]);
}

function dostats(chum)
{
	charstats[chum[0]] = new Object();
	for (var w=0; w<statlist.length; w++)
		charstats[chum[0]][statlist[w]] = chum[w];
	charstats[chum[0]].status = new Array();
}
init_charstats();
init_weapons();
//init_items();
//init_armor();
//init_relics();
//init_shit();



/*
//===================\\
 --- MONSTERS & GROUPS!! --- 
\\===================//
*/


var thastatlist = new Array("name", "lvl", "hp", "mp", "exp", "gp", "speed", "hit_rate", "mag_pow", "strength", "mag_def", "att_def", "mag_blk", "att_blk", "img");
var thegrouplist = new Array();
	
for (var v=1; v<=OpenFile("monster.dat").read("groupcount",0); v++)
	thegrouplist[v-1] = "group0" + ((v<10) ? "0" : "") + v;

function choosegroup(redrum)
{
	var thefile = OpenFile("monster.dat");

	var thegroup = new Object();
	if (redrum==undefined) thegroup.list = thefile.read(thegrouplist[Math.floor(Math.random()*thegrouplist.length)],"").split(";");
	else thegroup.list = thefile.read(thegrouplist[redrum],"").split(";");
	for (var g=0; g<thegroup.list.length; g++)
	{
		thegroup[g] = new Object();
		for (var f=0; f<thastatlist.length; f++)
		{
			thegroup[g][thastatlist[f]] = thefile.read(thegroup.list[g].split(",")[0], "").split(",")[f];
			if (f != 0 && f != thastatlist.length-1) thegroup[g][thastatlist[f]] *= 1;	//unstring'em
		}
		thegroup[g].x = thegroup.list[g].split(",")[1]*1;
		thegroup[g].y = thegroup.list[g].split(",")[2]*1;
	}
	thegroup.backattack = Math.round(Math.random()*2);
	thegroup.multiple = true;
	return thegroup;
}


function createenemy(alph, a,b, opface, backattack,owchable, eventrig)	//makes a group
{
	var megadude = new Object();
	for (var c=0; c<thastatlist.length; c++)
		megadude[thastatlist[c]] = alph[c];
	megadude['hp_max'] = megadude['hp'];
	megadude.x = a;
	megadude.y = b;
	megadude.opface = opface;
	megadude.owchable = owchable;
	
	var ultradude = new Object();
	ultradude[0] = megadude;
	ultradude.list = new Array(alph[0]);
	ultradude.backattack = backattack;
	ultradude.eventrig = eventrig;
	return ultradude;
}

function createenemy2(alph, a,b, opface, backattack,owchable)	//makes a guy w/o group
{
	var megadude = new Object();
	for (var c=0; c<thastatlist.length; c++)
		megadude[thastatlist[c]] = alph[c];
	megadude.x = a;
	megadude.y = b;
	megadude.opface = opface;
	megadude.owchable = owchable;
	return megadude;
}

function sologroup(stuff)
{
	var slayer = createenemy(OpenFile("monster.dat").read(stuff,"").split(","), 240,80, false, false, false);
	slayer.list = [stuff];
	slayer.multiple = true;
	return slayer;
}

//not *exactly* chronological
var theHUGEeventlist = new Array("prechase", "chasecourtjail", 'jail_scene', 'chase2', "furniture_mission", 'kefka_dream1', 
	'splits', "vicks_split", "wedge_split", "kefka_split", 'morning_after', "eavesdropped", "k_split_rampage", "tzen_welcome",
	"fitz_battle", "turtle_kick", 'aged_clock', "turtle_boss", "wedge_split_end", 'vicks_swindle', "vicks_buymteks", "sewerboss01",
	'transportmteks', 'splits_closing');
